import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import '../../core/models/action_record.dart';

/// 课程列表页面
class CourseListPage extends StatelessWidget {
  const CourseListPage({super.key});

  @override
  Widget build(BuildContext context) {
    // 从路由参数获取数据
    final Map<String, dynamic> args = Get.arguments ?? {};
    final String title = args['title'] ?? '课程列表';
    final List<ActionRecord> courses = args['courses'] ?? [];

    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        backgroundColor: Colors.black,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
          onPressed: () => Get.back(),
        ),
        title: Text(
          title,
          style: TextStyle(
            fontSize: 18.sp,
            color: Colors.white,
            fontWeight: FontWeight.w500,
          ),
        ),
        centerTitle: true,
        actions: [
          IconButton(
            icon: const Icon(Icons.menu, color: Colors.white),
            onPressed: () {
              // TODO: 显示菜单
            },
          ),
        ],
      ),
      body: courses.isEmpty
          ? Center(
              child: Text(
                '暂无课程',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: Colors.white70,
                ),
              ),
            )
          : Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 课程介绍
                Padding(
                  padding: EdgeInsets.all(16.w),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        '课程介绍',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white70,
                        ),
                      ),
                      SizedBox(height: 8.h),
                      Text(
                        '第一部分学习正念的练习方式',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: Colors.white70,
                        ),
                      ),
                    ],
                  ),
                ),
                // 课程列表
                Expanded(
                  child: ListView.builder(
                    padding: EdgeInsets.symmetric(horizontal: 16.w),
                    itemCount: courses.length,
                    itemBuilder: (context, index) {
                      final course = courses[index];
                      return _buildCourseItem(course, index);
                    },
                  ),
                ),
              ],
            ),
    );
  }

  Widget _buildCourseItem(ActionRecord course, int index) {
    return GestureDetector(
      onTap: () => _playCourse(course),
      child: Container(
        margin: EdgeInsets.only(bottom: 11.h),
        padding: EdgeInsets.all(10.w),
        decoration: BoxDecoration(
            color: const Color(0xFFffffff).withOpacity(0.04),
            borderRadius: BorderRadius.circular(12.r),
            border:
                Border.all(width: 1.w, color: Colors.white.withOpacity(0.08))),
        child: Row(
          children: [
            // 课程封面
            ClipRRect(
              borderRadius: BorderRadius.circular(8.r),
              child: course.fullImageUrl.isNotEmpty
                  ? Image.network(
                      course.fullImageUrl,
                      width: 80.w,
                      height: 80.w,
                      fit: BoxFit.cover,
                      errorBuilder: (context, error, stackTrace) {
                        return _buildDefaultCover();
                      },
                    )
                  : _buildDefaultCover(),
            ),
            SizedBox(width: 16.w),
            // 课程信息
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    course.actionName ?? '未命名课程',
                    style: TextStyle(
                      fontSize: 16.sp,
                      color: Colors.white,
                      fontWeight: FontWeight.w500,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  SizedBox(height: 8.h),
                  Text(
                    course.actionReadTime ?? '0:00:00',
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: Colors.white70,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildDefaultCover() {
    return Container(
      width: 64.w,
      height: 64.w,
      decoration: BoxDecoration(
        color: const Color(0xFF2A2A2A),
        borderRadius: BorderRadius.circular(16.r),
      ),
      child: Icon(
        Icons.image,
        color: Colors.white30,
        size: 32.sp,
      ),
    );
  }

  void _playCourse(ActionRecord course) {
    // 跳转到音频播放页面
    Get.toNamed('/audio-player', arguments: course);
  }
}
